package com.haisen.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.haisen.core.utils.PageResultSet;
import com.haisen.dataobject.entity.ItemParamKey;
import com.haisen.dataobject.mapper.base.ItemParamKeyMapper;
import com.haisen.dataobject.query.ItemParamKeyQuery;
import com.haisen.service.ItemParamGroupService;
import com.haisen.service.ItemParamKeyService;
import com.haisen.service.ItemParamValueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.*;

@Service
@Slf4j
public class ItemParamKeysServiceImpl implements ItemParamKeyService {

    @Autowired
    private ItemParamKeyMapper itemParamKeyMapper;


    @Override
    public PageResultSet<ItemParamKey> findByPage(ItemParamKeyQuery itemParamKeyQuery) {

        if (!StringUtils.isEmpty(itemParamKeyQuery.getOrderBy())) {
            PageHelper.orderBy(itemParamKeyQuery.getOrderBy());
        }
       //拼凑查询条件
        Weekend<ItemParamKey> weekend = Weekend.of(ItemParamKey.class);
        WeekendCriteria<ItemParamKey, Object> criteria = weekend.weekendCriteria();
        if (!StringUtils.isEmpty(itemParamKeyQuery.getName())) {
            criteria.andLike(ItemParamKey::getParamName, itemParamKeyQuery.getName());
        }
        List<ItemParamKey> itemParamKeyList = itemParamKeyMapper.selectByExample(weekend);
/*

        List<ItemParamKeyDTO> dtoList = new ArrayList<>();
        //使用mapkey来实现简单的匹配
        Map<Long,String> itemParamGroupMap = new HashMap<>();
        itemParamGroupService.findAll().forEach(g->{
            itemParamGroupMap.put(g.getId(),g.getGroupName());
        });

        itemParamKeyMapper.selectByExample(weekend).forEach(u -> {
            ItemParamKeyDTO dto = new ItemParamKeyDTO(u);
            dto.setGroupName(itemParamGroupMap.get(dto.getGroupId()));
            dtoList.add(dto);
        });
*/

        PageHelper.offsetPage(itemParamKeyQuery.getOffset(), itemParamKeyQuery.getLimit());
        long total = itemParamKeyMapper.selectCountByExample(weekend);
        PageResultSet<ItemParamKey> resultSet = new PageResultSet<>();
        resultSet.setRows(itemParamKeyList);
        resultSet.setTotal(total);
        return resultSet;
    }

    @Override
    public void create(ItemParamKey itemParamKey) {

        log.info("form:{}", JSON.toJSON(itemParamKey));
        //判断是否重复插入了
         ItemParamKey itemParamKey1 = itemParamKeyMapper.selectOne(itemParamKey);
        log.info("{}", JSON.toJSONString(itemParamKey1));
         /*if (itemParamKeyMapper.selectOne(itemParamKey) !=null){
             throw new BizException(ResultCodeEnum.FAILED_GROUP_ALREADY_EXIST);
         }*/
        itemParamKeyMapper.insertSelective(itemParamKey);
    }

    @Override
    public void update(ItemParamKey itemParamKey) {
       itemParamKeyMapper.updateByPrimaryKeySelective(itemParamKey);
    }

    @Override
    public void delete(Long id) {
        itemParamKeyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ItemParamKey findOneById(Long id) {
        return itemParamKeyMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ItemParamKey> findAll() {
        return itemParamKeyMapper.selectAll();
    }


    @Override
    public List<ItemParamKey> findByIds(List<Long> ids) {
        Weekend<ItemParamKey> example = Weekend.of(ItemParamKey.class);
        WeekendCriteria<ItemParamKey,Object> criteria = example.weekendCriteria();
        criteria.andIn(ItemParamKey :: getId,ids);
        StringBuilder s = new StringBuilder();
       return itemParamKeyMapper.selectByExample(example);
    }

    @Override
    public String findNames(List<Long> ids) {
          StringBuilder s = new StringBuilder();
                 findByIds(ids).forEach(o ->{
                     s.append(o.getParamName()+",");
                 });
        if (s.length() > 0) {
            s.deleteCharAt(s.length() - 1);
        }
        return s.toString();
    }


}
